package fr.pascal;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

@WebServlet("/servletBarChart")
public class ServletBarChart extends HttpServlet {

	private static final long serialVersionUID = -1263107913582411198L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(120000.0, "Produit 1", "2000");
		dataset.addValue(550000.0, "Produit 1", "2001");
		dataset.addValue(180000.0, "Produit 1", "2002");
		dataset.addValue(270000.0, "Produit 2", "2000");
		dataset.addValue(600000.0, "Produit 2", "2001");
		dataset.addValue(230000.0, "Produit 2", "2002");
		dataset.addValue(90000.0, "Produit 3", "2000");
		dataset.addValue(450000.0, "Produit 3", "2001");
		dataset.addValue(170000.0, "Produit 3", "2002");

		JFreeChart barChart = ChartFactory.createBarChart("Evolution des ventes", "",
				"Unité vendue", dataset, PlotOrientation.VERTICAL, true, true,
				false);

		OutputStream out = response.getOutputStream();
		response.setContentType("image/png");
		ChartUtilities.writeChartAsPNG(out, barChart, 400, 300);
	}
}